package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.UserDeductionRecordBo;
import com.joker.orange.domain.vo.UserDeductionRecordVo;
import com.joker.orange.domain.UserDeductionRecord;
import com.joker.orange.mapper.UserDeductionRecordMapper;
import com.joker.orange.service.IUserDeductionRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 扣单记录Service业务层处理
 *
 * @author Joker
 * @date 2024-07-27
 */
@RequiredArgsConstructor
@Service
public class UserDeductionRecordServiceImpl implements IUserDeductionRecordService {

    private final UserDeductionRecordMapper baseMapper;

    /**
     * 查询扣单记录
     */
    @Override
    public UserDeductionRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询扣单记录列表
     */
    @Override
    public TableDataInfo<UserDeductionRecordVo> queryPageList(UserDeductionRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserDeductionRecord> lqw = buildQueryWrapper(bo);
        Page<UserDeductionRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询扣单记录列表
     */
    @Override
    public List<UserDeductionRecordVo> queryList(UserDeductionRecordBo bo) {
        LambdaQueryWrapper<UserDeductionRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserDeductionRecord> buildQueryWrapper(UserDeductionRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserDeductionRecord> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), UserDeductionRecord::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getAgentName()), UserDeductionRecord::getAgentName, bo.getAgentName());
        lqw.eq(bo.getChargeMoney() != null, UserDeductionRecord::getChargeMoney, bo.getChargeMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getChargeType()), UserDeductionRecord::getChargeType, bo.getChargeType());
        return lqw;
    }

    /**
     * 新增扣单记录
     */
    @Override
    public Boolean insertByBo(UserDeductionRecordBo bo) {
        UserDeductionRecord add = BeanUtil.toBean(bo, UserDeductionRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改扣单记录
     */
    @Override
    public Boolean updateByBo(UserDeductionRecordBo bo) {
        UserDeductionRecord update = BeanUtil.toBean(bo, UserDeductionRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserDeductionRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除扣单记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
